package own.stu.jobgib.playown.alg.leetcode.editor.cn;

//给定一个二叉树的根节点 root ，返回它的 中序 遍历。 
//
// 
//
// 示例 1： 
//
// 
//输入：root = [1,null,2,3]
//输出：[1,3,2]
// 
//
// 示例 2： 
//
// 
//输入：root = []
//输出：[]
// 
//
// 示例 3： 
//
// 
//输入：root = [1]
//输出：[1]
// 
//
// 示例 4： 
//
// 
//输入：root = [1,2]
//输出：[2,1]
// 
//
// 示例 5： 
//
// 
//输入：root = [1,null,2]
//输出：[1,2]
// 
//
// 
//
// 提示： 
//
// 
// 树中节点数目在范围 [0, 100] 内 
// -100 <= Node.val <= 100 
// 
//
// 
//
// 进阶: 递归算法很简单，你可以通过迭代算法完成吗？ 
// Related Topics 栈 树 深度优先搜索 二叉树 
// 👍 1013 👎 0


import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class BinaryTreeInorderTraversal{
    public static void main(String[] args) {
        Solution solution = new BinaryTreeInorderTraversal().new Solution();
        
    }
    //leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        if(root == null){
            return result;
        }

        //        midOrder(root, result); // 递归

        Stack<TreeNode> stack = new Stack<>();
        while(root != null || !stack.isEmpty()){
            while (root != null) {
                stack.push(root);
                root = root.left;
            }
            root = stack.pop();
            result.add(root.val);
            root = root.right;
        }

        return result;
    }

    public List<Integer> inorderTraversal_bak(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        if(root == null){
            return result;
        }

        //        midOrder(root, result); // 递归

        Stack<Node> stack = new Stack<>();
        stack.push(new Node(root, 0));

        while(!stack.isEmpty()){
            Node tmp = stack.pop();
            TreeNode treeNode = tmp.node;
            if(treeNode == null){
                continue;
            }

            if(tmp.flag == 1){
                result.add(treeNode.val);
                continue;
            }
            stack.push(new Node(treeNode.right, 0));
            stack.push(new Node(treeNode, 1));
            stack.push(new Node(treeNode.left, 0));
        }

        return result;
    }

    class Node{
        TreeNode node;
        int flag; // 0- add, 1 -print

        public Node(TreeNode node, int flag){
            this.node = node;
            this.flag = flag;
        }
    }

    private void midOrder(TreeNode root, List<Integer> result){
        if(root == null){
            return;
        }

        midOrder(root.left, result);
        result.add(root.val);
        midOrder(root.right, result);
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}
